import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { faker } from "@faker-js/faker/locale/zh_CN";

const data: any[] = [];
const catalogs: any[] = [];

for (let i = 0; i < 20; i++) {
  const format = faker.book.format();
  if (
    catalogs.length < 10 &&
    catalogs.findIndex(item => item.name === format) < 0
  ) {
    catalogs.push({
      id: i,
      name: format
    });
  }
}

for (let i = 0; i < 100; i++) {
  data.push({
    id: i + 1,
    catalog: catalogs[Math.floor(Math.random() * catalogs.length)],
    title: faker.book.title(),
    author: faker.book.author(),
    remark: faker.book.genre(),
    sort: "99",
    status: Math.random() > 0.5 ? true : false,
    createTime: faker.date.between({ from: "2000-01-01", to: Date.now() })
  });
}

export default defineFakeRoute([
  {
    url: "/template/table/catalogs",
    method: "get",
    response: () => {
      return {
        success: true,
        message: "操作成功",
        data: catalogs
      };
    }
  },
  {
    url: "/template/table/page",
    method: "post",
    //statusCode: 404,
    response: ({ body }) => {
      return {
        success: true,
        message: "操作成功",
        data: {
          list: data.slice(
            (body.page - 1) * body.pageSize,
            body.page * body.pageSize
          ),
          total: data.length,
          pageSize: body.pageSize,
          currentPage: body.page
        }
      };
    }
  },
  {
    url: "/template/table",
    method: "post",
    response: ({ body }) => {
      return {
        success: true,
        message: "操作成功",
        data: {
          ...body,
          id: 999
        }
      };
    }
  },
  {
    url: "/template/table/:id",
    method: "get",
    response: ({ params }) => {
      return {
        success: true,
        message: "操作成功",
        data: data.find(item => item.id == params.id)
      };
    }
  },
  {
    url: "/template/table/:id",
    method: "put",
    response: ({ params }) => {
      return {
        success: true,
        message: "操作成功",
        data: data.find(item => item.id == params.id)
      };
    }
  },
  {
    url: "/template/table/:id/status",
    method: "put",
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/template/table/:id",
    method: "delete",
    statusCode: 404,
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/template/table",
    method: "delete",
    response: () => {
      return {
        success: false,
        message: "禁止删除"
      };
    }
  }
]);
